<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_SHOP.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_SHOP,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_M_SHOP;


if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['SHOP'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['SHOP'] = $forms;
}
$login_auth = $login['staff_auth'];
if ($login_auth == 'G-2' ) {
	$area_cd   = $login['autoid'];
	$forms["sort"] = '2';
	$areauser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else if ($login_auth == 'G-3' ) {
	$zone_cd   = $login['autoid'];
	$forms["sort"] = '3';
	$cellsort = array();
	$zone_cd = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else {
	$zoneuser  = array();
	$zoneuser  = Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 3);
	$areauser = array();
	$areauser =  Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 2);
	$zone_cd = array();
}
$wherearr =  array();

if ( isset( $forms['local_cd'] ) &&  ( !empty( $forms['local_cd'] ) ) ){
	$wherearr[] = sprintf(" a.local_cd =  '%s' ",  $dao->esc( $forms['local_cd'] ) );
}
if  (($forms["sort"] == '2') &&  $forms["area_aja"]) {
	
	$wherearr[] = sprintf(" a.area_aja =  %s ",  $dao->esc( $forms['area_aja'] ) );
} 

if  (($forms["sort"] == '3') &&  $forms["zone_aja"]) {
	
	$wherearr[] = sprintf(" a.zone_aja =  %s ",  $dao->esc( $forms['zone_aja'] ) );
} 

if ( isset( $forms['block_cd'] ) &&  ( !empty( $forms['block_cd'] ) ) ){
	$wherearr[] = sprintf(" a.block_cd =  '%s' ", $dao->esc(  $forms['block_cd'] ) );
}

if ( isset( $forms['shop_cd'] ) &&  ( !empty( $forms['shop_cd'] ) ) ){
	$wherearr[] = sprintf(" a.shop_cd =  '%s' ", $dao->esc(  $forms['shop_cd'] ) );
}

if ( isset( $forms['shop_name'] ) &&  ( !empty( $forms['shop_name'] ) ) ){
	$wherearr[] = sprintf(" a.shop_name like  '%s' ",  "%".$dao->esc( $forms['shop_name'] )."%" );
}
/*
if ( isset( $forms['smx']  )  &&  ( !empty( $forms['smx'] ) ) ){
	$wherearr[] = sprintf(" smx =  '%s' ", $dao->esc(  $forms['smx'] ) );
}

if ( isset( $forms['center']  )  &&  ( !empty( $forms['center'] ) ) ){
	$wherearr[] = sprintf(" a.center =  '%s' ",  $forms['center'] );
}
*/
if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, 'a' );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2, 'a' );
}

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "shop".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->searchcsv( $wherearr );

//列名
	$colums = array();

//1 string code 2 integer 3 date 4 string ""必要 
	$col_heads = array(
		'shop_cd'      => array( '店舗コード',      1),
		'shop_name'    => array( '店舗名',          1),
		'block_cd'     => array( '地域ブロックコード',1),
		'block_name'   => array( '地域ブロック名',    1),
		'local_cd'     => array( '都道府県コード',    1),
		'local_name'   => array( '都道府県名',        1),
		'postcode'     => array( '郵便番号',          1),
		'address'      => array( '住所',              1),
		'tel'          => array( '電話番号',          1),
		'lnews1'       => array( '地域新聞名１コード',1),
		'lnews1_name'  => array( '地域新聞名１名称',  1),
		'lnews2'       => array( '地域新聞名３コード',1),
		'lnews2_name'  => array( '地域新聞名２名称',  1),
		'lnews3'       => array( '地域新聞名３コード',1),
		'lnews3_name'  => array( '地域新聞名３名称',  1),
		'lnews4'       => array( '地域新聞名４コード',1),
		'lnews4_name'  => array( '地域新聞名４名称',  1),
		'lnews5'       => array( '地域新聞名５コード',1),
		'lnews5_name'  => array( '地域新聞名５名称',  1),
		'memo'         => array( '備考',  1),
		'year_sale'    => array( '年商(億円)',  1),
		'stop_date'    => array( '廃止日',            3),
		'zone_aja_cd'  => array( 'ゾーンAJAコード',  1),
		'zone_aja_nm'  => array( 'ゾーンAJA名',  1),
		'area_aja_cd'  => array( 'エリアAJAコード',  1),
		'area_aja_nm'  => array( 'エリアAJA名',  1),
		'u_time'       => array( '更新日時',          3),
		'c_time'       => array( '登録日時',          3),
		'm_user_name'  => array( '更新者',            1)
	);


	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	
	$zoneareauser  = Class_M_OTHER::get_ajaeks_array("CD-NAME", NULL); 
	$media = Class_M_OTHER::get_pattern_array(NULL, 23);
	foreach ($data as $k => $v) {
		//$data[$k]["center_name"] =  $aoki_set_flags[$v["center"]];
		
		$data[$k]["lnews1_name"] = $media[$v["lnews1"]] ;
		$data[$k]["lnews2_name"] = $media[$v["lnews2"]] ;
		$data[$k]["lnews3_name"] = $media[$v["lnews3"]] ;
		$data[$k]["lnews4_name"] = $media[$v["lnews4"]] ;
		$data[$k]["lnews5_name"] = $media[$v["lnews5"]] ;
		
		$data[$k]["zone_aja_cd"]   = $zoneareauser[$v["zone_aja"]]["cd"];
		$data[$k]["zone_aja_nm"]   = $zoneareauser[$v["zone_aja"]]["name"];
		
		$data[$k]["area_aja_cd"]   = $zoneareauser[$v["area_aja"]]["cd"];
		$data[$k]["area_aja_nm"]   = $zoneareauser[$v["area_aja"]]["name"];
				
		$data[$k]["stop_date"]   =  Class_PWD::int8_to_date($v["stop_date"]);
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
}

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);

$block = Class_M_OTHER::get_block_array( PLEASE_SELECT_ARR );
$smarty->assign('block', $block );

$local= Class_M_OTHER::get_local_array( PLEASE_SELECT_ARR );
$smarty->assign('local', $local );

$flags[""] = "すべて";
foreach ($aoki_set_flags as $k => $v) {
	$flags[$k] = $v;
}


	$aoki_area_zones = array();
	$aoki_area_zones[""] = "すべて";
	foreach ($aoki_area_zone as $k => $v) {
		$aoki_area_zones[$k] = $v;
	}
	$smarty->assign('sorts', $aoki_area_zones );
	
	if( isset($zoneuser) )    $smarty->assign('zoneuser', $zoneuser);
	if( isset($areauser) )    $smarty->assign('areauser', $areauser);

$smarty->assign('flags', $flags );

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mshop_index.html');

?>